package finalproject.model.player;

import java.util.ArrayList;
import finalproject.model.items.CheatSheet;
import finalproject.model.items.Item;
import finalproject.model.player.pet.Pet;
import finalproject.model.rooms.Room;
import finalproject.model.studies.Lab;
import finalproject.model.studies.Lecture;
import finalproject.util.datastructure.LectureLabAssociation;

public class Player {

	private String nickname;
	private Room currentRoom;
	private Path playerPath;
	private ItemManager itemHolder;
	private Badge energy;
	private CourseBook book;
	private boolean finished;
	private Pet pet;

	public Player(String nickname, Room startingRoom, Pet pet)
	{
		this(nickname, startingRoom, false, new Path(), new ItemManager(new ArrayList<Item>(), 20, 0,
				new LectureLabAssociation()), new Badge(0, new Energy(10)), new CourseBook(new ArrayList<Lecture>(),
				new ArrayList<Lab>()), pet);
		//TODO add call constructor ability
	}

	public Player(String nickname, Room currentRoom, boolean finished, Path playerPath, ItemManager itemHolder,
			Badge energy, CourseBook book, Pet pet)
	{
		this.nickname = nickname;
		this.currentRoom = currentRoom;
		this.finished = finished;
		this.playerPath = playerPath;
		this.itemHolder = itemHolder;
		this.energy = energy;
		this.book = book;
		this.pet = pet;
	}

	/**
	 * @return the nickname
	 */
	public String getNickname()
	{
		return nickname;
	}

	/**
	 * @return the currentRoom
	 */
	public Room getCurrentRoom()
	{
		return currentRoom;
	}

	/**
	 * @param currentRoom
	 *            the currentRoom to set
	 */
	public boolean getIn(Room newRoom)
	{
		if (currentRoom.leavable(this))
		{
			playerPath.addRoom(currentRoom);
			this.currentRoom = newRoom;

			return true;
		}
		return false;
	}

	public boolean goBack()
	{
		Room lastRoom;
		if (currentRoom.leavable(this))
		{
			lastRoom = playerPath.goBack();
			if (lastRoom == null)
			{
				return false;
			}
			else
			{
				this.currentRoom = lastRoom;
				return true;
			}
		}
		return false;
	}

	/**
	 * @return the energy
	 */
	public Energy getEnergy()
	{
		return energy.getEnergie();
	}

	/**
	 * @param energy
	 *            the energy to set
	 */
	public void setEnergy(Energy energy)
	{
		this.energy.setEnergie(energy);
	}

	public boolean takeItem(String secondWord)
	{
		return itemHolder.putItemFrom(secondWord, currentRoom);
	}

	public boolean dropItem(String secondWord)
	{
		return itemHolder.drop(secondWord, currentRoom);
	}

	public boolean isCheater()
	{
		for (Item item : itemHolder.getHeldItems())
		{
			if (item instanceof CheatSheet)
			{
				return true;
			}
		}

		return false;
	}

	public String listItem()
	{
		String inventory = "";
		for (Item item : itemHolder.getHeldItems())
		{
			inventory += item.toString() + "\n";
		}
		if (itemHolder.getWorkingManager().size() != 0)
		{
			for (int i = 0; i < itemHolder.getWorkingManager().size(); i++)
			{
				if (itemHolder.getWorkingManager().getValue(i) != null)
				{
					inventory += itemHolder.getWorkingManager().getKey(i) + " "
							+ itemHolder.getWorkingManager().getValue(i);
				}
				else
				{
					inventory += itemHolder.getWorkingManager().getKey(i);
				}
			}
		}
		if (inventory.equals(""))
		{
			inventory += "you have nothing now.";
		}
		return inventory;
	}

	/**
	 * @return the itemHolder
	 */
	public ItemManager getItemHolder()
	{
		return itemHolder;
	}

	/**
	 * @param itemHolder
	 *            the itemHolder to set
	 */
	public void setItemHolder(ItemManager itemHolder)
	{
		this.itemHolder = itemHolder;
	}

	public CourseBook getBook()
	{
		return book;
	}

	public void setBook(CourseBook book)
	{
		this.book = book;
	}

	public Badge getBadge()
	{
		return energy;
	}

	public boolean hasFinished()
	{
		return finished;
	}

	public void setFinished(boolean finished)
	{
		this.finished = finished;
	}

	public Pet getPet()
	{
		return pet;
	}

	public void setPet(Pet pet)
	{
		this.pet = pet;
	}


	public boolean energyIsMax()
	{
		return energy.getEnergie().isMax();
	}
	
	public String toString(){
		String nameDisplay = "Name: " + nickname + "\n";
		String energyDisplay = "Energy: " + energy.getEnergie() + "\n";
	
		return nameDisplay + energyDisplay;
	}
}
